﻿using DocumentFormat.OpenXml.Lite.DrawingML.Text;
using DocumentFormat.OpenXml.Lite.DrawingML.Text.Properties;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.DrawingML.Chart
{
    [XmlRoot("tx", Namespace = Constants.Chart)]
    public class Text : XmlSerializable
    {
        [XmlElement("", Namespace = Constants.Chart)]
        public IChartTextElement TextElement { get; set; }

        public Text() { }
    }

    [XmlRoot("v", Namespace = Constants.Chart)]
    public class TextValue : XmlSerializable, IChartTextElement
    {
        [XmlText]
        public string Value { get; set; }

        public TextValue() { }
    }

    [XmlRoot("strCache", Namespace = Constants.Chart)]
    public class StringCache : XmlSerializable
    {
        [Optional]
        [XmlElement("ptCount", Namespace = Constants.Chart)]
        public NullableValue<uint> PointCount { get; set; }

        [Optional]
        [XmlElement("pt", Namespace = Constants.Chart)]
        public XmlList<Point> Point { get; set; }

        [Optional]
        [XmlElement("extLst", Namespace = Constants.Chart)]
        public ExtensionList ExtensionList { get; set; }

        public StringCache() 
        {
            Point = new XmlList<Point>();
        }
    }
    
    [XmlRoot("pt", Namespace = Constants.Chart)]
    public class Point : XmlSerializable
    {
        [XmlAttribute("idx", Form = XmlSchemaForm.Qualified)]
        public uint Index { get; set; }

        [Optional]
        [XmlAttribute("formatCode", Form = XmlSchemaForm.Qualified)]
        public string FormatCode { get; set; }

        [XmlElement("v", Namespace = Constants.Chart)]
        public SimpleText Value { get; set; }

        public Point() { }
    }
    
    [XmlRoot("rich", Namespace = Constants.Chart)]
    public class RichText : XmlSerializable, IChartTextElement
    {
        [XmlElement("bodyPr", Namespace = Constants.Main)]
        public BodyProperties BodyProperties { get; set; }

        [Optional]
        [XmlElement("lstStyle", Namespace = Constants.Main)]
        public TextListStyles TextListStyles { get; set; }

        [XmlElement("p", Namespace = Constants.Main)]
        public XmlList<Paragraph> Paragraphs { get; set; }

        public RichText() 
        {
            Paragraphs = new XmlList<Paragraph>();
        }

        public RichText(string text, RunProperties properties)
        {
            Paragraphs = new XmlList<Paragraph>();
            Paragraph p = new Paragraph();
            Run r = new Run() { Text = new SimpleText(text) };
            r.Properties = properties;
            p.Elements.Add(r);
            Paragraphs.Add(p);
        }
    }
}
